package com.tiankun.eurekausermsg.service.impl;

import com.tiankun.eurekausermsg.exception.BusinessException;
import com.tiankun.eurekausermsg.mapper.UserMapper;
import com.tiankun.eurekausermsg.pojo.User;
import com.tiankun.eurekausermsg.service.UserService;
import com.tiankun.eurekausermsg.util.JsonResult;
import com.tiankun.eurekausermsg.util.MD5Util;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2020/11/10 10:22
 * @Version 1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public Map insert(String phone, String password, String yzm) {

        System.out.println("获取的电话："+phone);
        System.out.println("用户输入的验证码："+yzm);

        //该手机号是否已注册用户
        JsonResult jsonResult = new JsonResult();

        int i = userMapper.selectByPhone(phone);

        //如果已注册，提示用户已注册、更换手机号或者直接登陆
        if(i != 0){
            throw new BusinessException("该手机号已注册，请更换手机号或者直接登陆");
        }
        //判断验证码是否合法
        //从redis中获取验证码
        BoundValueOperations<String, Object> op = redisTemplate.boundValueOps(phone);
        Object o = op.get();
//        ValueOperations<String, Object> ob = redisTemplate.opsForValue();
//        Object o = ob.get(phone);

        System.out.println("获取的验证码:"+o);

        if (yzm.equals(o)) {
            //如果合法，将个人信息+注册时间添加到数据库

            //创建盐
            String salt = MD5Util.createSalt();
            //加密密码、并存储加密后的密码和盐
            String encode = MD5Util.encode(password + salt);
            userMapper.insertInfo(phone,encode,new Date(),salt);

            jsonResult.setCode("200");
            jsonResult.setMsg("注册成功");
        } else {
            //如果不合法，返回注册失败，验证码错误
            jsonResult.setCode("400");
            jsonResult.setMsg("验证码不合法");
        }

        return jsonResult.getValues();
    }

    @Override
    public Map login(String phone, String password) {

        System.out.println(phone);
        System.out.println(password);
        //通过电话判断该用户是否已经注册
        int i = userMapper.selectByPhone(phone);
        System.out.println(i);

        if (i == 0){
            throw new BusinessException("您还未注册，请先注册");
        }

        //对比密码是否一致
        String findpwd = userMapper.findpwd(phone);
        System.out.println("数据库的密码："+findpwd);
        //获取数据库用户的盐
        String salt = userMapper.getsalt(phone);
        //加密密码、并存储加密后的密码和盐
        String encode = MD5Util.encode(password + salt);
        System.out.println("用户输入加密后的密码："+encode);
        if (!(encode.equals(findpwd))){
            throw new BusinessException("密码不正确");
        }

        /*待补充业务
        1.用户拿到的权限

        2.单点登录

        * */

        Map byPhone = userMapper.findByPhone(phone);

        System.out.println("返回的数据："+byPhone);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功");
        jsonResult.setData(byPhone);
        return jsonResult.getValues();
    }

    @Override
    public Map changepwd(String password, String phone,String yzm) {

        System.out.println(phone+"  "+password+"  "+yzm);

        JsonResult jsonResult = new JsonResult();

        //判断验证码是否合法
        //从redis中获取验证码
        BoundValueOperations<String, Object> op = redisTemplate.boundValueOps(phone);
        Object o = op.get();
//        ValueOperations<String, Object> ob = redisTemplate.opsForValue();
//        Object o = ob.get(phone);

        System.out.println("获取的验证码:"+o);

        if (yzm.equals(o)) {
            //如果合法，将个人信息+注册时间添加到数据库

            //创建盐
            String salt = MD5Util.createSalt();
            //加密密码、并存储加密后的密码和盐
            String encode = MD5Util.encode(password + salt);
            System.out.println("加密后的密码"+encode);
            userMapper.changepwd(encode,salt,phone);

            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        } else {
            //如果不合法，返回注册失败，验证码错误
            jsonResult.setCode("400");
            jsonResult.setMsg("验证码不合法");
        }


        return jsonResult.getValues();
    }


}
